package com.qf.service.impl;

import com.qf.dao.impl.ProductDaoImpl;
import com.qf.dao.ProductDao;
import com.qf.entity.Product;
import com.qf.service.ProductService;
import com.qf.utils.PageUtils;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @title:ProductServiceImpl
 * @Descripe:
 * @author:Wjl
 * @date:2023/4/20 17:14
 */
public class ProductServiceImpl implements ProductService {
    private ProductDao productDao = new ProductDaoImpl();

    /**
     *根据条件查询全部的商品(商品类型，商品状态，商品名,当前页和显示数)
     * @param ps_idStr：前端的value值
     * @param pt_idStr：前端的value值
     * @param pro_name：前端的value值
     * @param currentPageNoStr：前端的value值
     * @param pageSizeStr：前端的value值
     * @return
     */
    @Override
    public PageUtils<Product> selectAllProduct(String ps_idStr, String pt_idStr, String pro_name, String currentPageNoStr, String pageSizeStr) {

        //把String类型转换成相应的数据类型
        Integer ps_id = null;
        Integer pt_id = null;
        if (ps_idStr!=null&&!"".equals(ps_idStr)){
            ps_id = Integer.parseInt(ps_idStr);
        }

        if (pt_idStr!=null&&!"".equals(pt_idStr)){
            pt_id = Integer.parseInt(pt_idStr);
        }

        //获取第几页
        Integer currentPageNo=1;
        if (currentPageNo==null||"".equals(currentPageNo)){
            currentPageNo=1;
        }

        //获取当前页的条数
        Integer pageSize=10;
        if (pageSizeStr==null||"".equals(pageSizeStr)){
            pageSize=10;
        }

        //获取当前条件的全部数据总条数(所有页的数据条数)
        Integer totalPageCount = null;
        try {//调用查询数据条数的dao里面的方法
            totalPageCount=productDao.DataCount(ps_id,pt_id,pro_name);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        //获取总页数
        Integer totalPageSize = null;
        if (pageSize%totalPageCount==0){
            totalPageSize=pageSize/totalPageCount;
        }else {
            totalPageSize=pageSize/totalPageCount+1;
        }

        //获取所有页复合条件的的数据
        List<Product> products = null;
        try {
            products = productDao.selectAllProduct(ps_id, pt_id, pro_name, currentPageNo, pageSize);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        //将第几页，当前页记录条数，当前条件的总数据条数，获取总页数,总数据存入到分页工具类
        PageUtils pageUtils =new PageUtils(currentPageNo,pageSize,totalPageCount,totalPageCount,products);

        System.out.println("第几页："+currentPageNo);
        System.out.println("当前页显示数据条数："+pageSize);
        System.out.println("总数据条数："+totalPageCount);
        System.out.println("总页数："+totalPageSize);
        System.out.println(products.toString());
        return pageUtils;

    }

    /**
     * 通过id或者商品名查询商品详细数据
     * @param pro_idStr
     * @param pro_name
     * @return
     * @throws SQLException
     */
    @Override
    public Product selectById_Or_Name(String pro_idStr, String pro_name) {
        if (pro_idStr==null&&"".equals(pro_idStr)&&pro_name==null&&"".equals(pro_name)){
            return null;
        }

        Integer pro_id = null;
        if (pro_idStr!=null||!"".equals(pro_idStr)){
            pro_id = Integer.parseInt(pro_idStr);
        }


        try {
            return productDao.selectById_Or_Name(pro_id,pro_name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     *添加商品
     * @param pro_name：商品名
     * @param pro_photo：商品图片地址
     * @param ps_idStr：商品状态id
     * @param pro_purchasing_priceStr：商品进货价
     * @param pro_sale_priceStr：商品售价
     * @param pro_numberStr：商品库存
     * @param pt_idStr：商品类型
     * @return
     * @throws SQLException
     */
    @Override
    public int addProduct(String pro_name, String pro_photo, String ps_idStr, String pro_purchasing_priceStr, String pro_sale_priceStr, String pro_numberStr, String pt_idStr, Date pro_create){
        //判断是否已存在改商品
        Product product = selectById_Or_Name(null, pro_name);

        if (product==null&&"".equals(product)){
            Integer ps_id=null;
            Double pro_purchasing_price=null;
            Double pro_sale_price=null;
            Integer pro_number=null;
            Integer pt_id=null;
            try {
                if (ps_idStr!=null&&!"".equals(ps_idStr)){
                    ps_id=Integer.parseInt(ps_idStr);
                }
                if (pro_purchasing_priceStr!=null&&!"".equals(pro_purchasing_priceStr)){
                    pro_purchasing_price=Double.parseDouble(pro_purchasing_priceStr);
                }
                if (pro_sale_priceStr!=null&&!"".equals(pro_sale_priceStr)){
                    pro_sale_price =Double.parseDouble(pro_sale_priceStr);
                }
                if (pro_numberStr!=null&&!"".equals(pro_numberStr)){
                    pro_number=Integer.parseInt(pro_numberStr);
                }
                if (pt_idStr!=null&&!"".equals(pt_idStr)){
                    pt_id=Integer.parseInt(pt_idStr);
                }
            } catch (NumberFormatException e) {
                e.printStackTrace();
                return 0;
            }


            if (pro_name==null||"".equals(pro_name)|| pro_photo==null||"".equals(pro_photo)){
                return 0;
            }

            try {
                return productDao.addProduct(pro_name,pro_photo,ps_id,pro_purchasing_price,pro_sale_price,pro_number,pt_id,pro_create);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return 0;
    }

    /**
     * 通过id删除用户
     * @param pro_idStr
     * @return
     */
    @Override
    public int delProduct(String pro_idStr) {

        Product product = selectById_Or_Name(pro_idStr,null);

        if (product==null&&"".equals(product)){
            return 0;
        }else {
            try {
                return productDao.delProduct(Integer.parseInt(pro_idStr));
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return 0;
    }

    /**
     *
     * @return
     */
    @Override
    public int updateProduct(Map<String,String> proMap) {


        if (proMap==null&&proMap.size()<=0){
            return 0;
        }else {
            int pro_id=0;
            int ps_id=0;
            double pro_purchasing_price=0;
            double pro_sale_price=0;
            int pro_number=0;
            int pt_id=0;
            try {
                if (proMap.get("pro_id")!=null&&!"".equals(proMap.get(proMap.get("pro_id")))){
                    pro_id=Integer.parseInt(proMap.get(proMap.get("pro_id")));
                }
                if (proMap.get("ps_id")!=null&&!"".equals(proMap.get("ps_id"))){
                    ps_id=Integer.parseInt(proMap.get("ps_id"));
                }
                if (proMap.get("pro_purchasing_price")!=null&&!"".equals(proMap.get("pro_purchasing_price"))){
                    pro_purchasing_price=Double.parseDouble(proMap.get("pro_purchasing_price"));
                }
                if (proMap.get("pro_sale_price")!=null&&!"".equals(proMap.get("pro_sale_price"))){
                    pro_sale_price =Double.parseDouble(proMap.get("pro_sale_price"));
                }
                if (proMap.get("pro_number")!=null&&!"".equals(proMap.get("pro_number"))){
                    pro_number=Integer.parseInt(proMap.get("pro_number"));
                }
                if (proMap.get("pt_id")!=null&&!"".equals(proMap.get("pt_id"))){
                    pt_id=Integer.parseInt(proMap.get("pt_id"));
                }

                Product product = new Product();
                product.setPro_id(pro_id);
                product.setPro_name(proMap.get("pro_name"));
                product.setPro_photo(proMap.get("pro_photo"));
                product.setPs_id(ps_id);
                product.setPro_purchasing_price(pro_purchasing_price);
                product.setPro_sale_price(pro_sale_price);
                product.setPro_number(pro_number);
                product.setPt_id(pt_id);

                try {
                    productDao.updateProduct(product);
                } catch (SQLException e) {
                    e.printStackTrace();
                    return 0;
                }
            } catch (NumberFormatException e) {
                e.printStackTrace();
                return 0;
            }
        }
        return 0;
    }
}
